<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html>
<html xmlns:th="" lang="zh-CN">
<%@include file="/WEB-INF/pages/base/include.jsp"%>
<body>
<div id="wrapper">
    <%@include file="/WEB-INF/pages/base/menu.jsp"%>
    <div id="page-wrapper">
        <div class="row">
            <div class="col-lg-12">
                <h3 class="page-header">红包管理</h3>
            </div>
        </div>
        <div class="row">
            <form class="form-horizontal" data-parsley-validate>
                <div class="col-lg-12 col-md-12">
                    <div class="panel panel-default">
                        <div class="panel-heading">红包基本信息</div>
                        <div class="panel-body">
                            <div class="row">
                                <div class="col-lg-12 col-md-12">
                                	<div class="form-group">
                                        <label class="col-lg-2 clo-md-2 control-label">红包名称：</label>
                                        <div class="col-lg-8">
                                            <input type="text" id="name" class="form-control" placeholder="请输入红包名称"
                                                   required>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-lg-2 clo-md-2 control-label">奖励条件：</label>
                                        <div class="col-lg-8">
                                            <input type="text" id="conditionCash" class="form-control" placeholder="请输入奖励条件"
                                                   required>
                                        </div>
                                        <div class="col-lg-2 clo-md-2">
                                            <span class="help-block">*必须填写</span>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-lg-2 clo-md-2 control-label">红包金额：</label>
                                        <div class="col-lg-8">
                                            <input type="text" id="rewardCash" class="form-control"
                                                   placeholder="请输入红包金额">
                                        </div>
                                        <div class="col-lg-2 clo-md-2">
                                            <span class="help-block">*必须填写</span>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-lg-2 clo-md-2 control-label">使用规则：</label>
                                        <div class="col-lg-8">
                                            满 <input type="text" id="useCashCondition" > 元，可使用 <input type="text" id="useCashResult" > 元
                                        </div>
                                        <div class="col-lg-2 clo-md-2">
                                            <span class="help-block">*必须填写</span>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-lg-2 clo-md-2 control-label">红包类型：</label>
                                        <div class="col-lg-8">
	                                        <select class="form-control" id="typeList">
		                                            
	                                        </select>
                                    	</div>
                                    	<div class="col-lg-2 clo-md-2">
                                            <span class="help-block">*必须选择</span>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-lg-2 clo-md-2 control-label">专用红包绑定商品id：</label>
                                        <div class="col-lg-8">
                                            <input type="text" id="productId" class="form-control"
                                                   placeholder="请输入专用红包绑定商品id">
                                        </div>
                                        <div class="col-lg-2 clo-md-2">
                                            <span class="help-block">*若选择专用红包必须填写</span>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-lg-2 clo-md-2 control-label">限制数量：</label>
                                        <div class="col-lg-8">
                                            <input type="text" id="limitCount" class="form-control"
                                                   placeholder="请输入限制领取数量">
                                        </div>
                                        <div class="col-lg-2 clo-md-2">
                                            <span class="help-block">*必须填写</span>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-lg-2 clo-md-2 control-label">状态：</label>
                                        <div class="col-lg-8">
	                                        <select class="form-control" id="isActive">
		                                            <option value="0">冻结</option>
		                                            <option value="1">激活</option>
	                                        </select>
                                    	</div>
                                    	<div class="col-lg-2 clo-md-2">
                                            <span class="help-block">*必须选择</span>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-lg-2 clo-md-2 control-label">开始时间：</label>
                                        <div class="col-lg-8">
                                            <input class="form-control" id="startTime" type="text" placeholder="开始时间">
                                        </div>
                                        <div class="col-lg-2 clo-md-2">
                                            <span class="help-block">*必须选择</span>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-lg-2 clo-md-2 control-label">结束时间：</label>
                                        <div class="col-lg-8">
                                            <input class="form-control" id="endingTime" type="text" placeholder="结束时间">
                                        </div>
                                        <div class="col-lg-2 clo-md-2">
                                            <span class="help-block">*必须选择</span>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-lg-2 clo-md-2 control-label">创建时间：</label>
                                        <div class="col-lg-8">
                                            <input type="text" id="createTime" class="form-control" readOnly>
                                        </div>
                                    </div>
                                    <div class="form-group" id="sale_mall">
                                        <label class="col-lg-2 clo-md-2 control-label">红包销售(商城)：</label>
                                        <div class="col-lg-8" id="mallList">
                                        	<c:forEach var="item" items="${mallList}" varStatus="status">
						                             <input type="checkbox" name="saleMallArea" value="${item.mallUuid}">${item.mallName}
						                    </c:forEach>
                                        </div>
                                    </div>
                                    <div class="form-group" id="sale_seller">
                                        <label class="col-lg-2 clo-md-2 control-label">红包销售(供应商)：</label>
                                        <div class="col-lg-8" id="sellerList">
                                        	<c:forEach var="item" items="${productSellerList}" varStatus="status">
						                             <input type="checkbox" name="saleSellerArea" value="${item.productSellerId}">${item.productSellerName}
						                    </c:forEach>
                                        </div>
                                    </div>
                                    <div class="form-group" id="sale_category">
                                        <label class="col-lg-2 clo-md-2 control-label">红包销售(分类)：</label>
                                        <div class="col-lg-8" id="categoryList">
                                        	<%-- <c:forEach var="item" items="${mallList}" varStatus="status">
						                             <input type="checkbox" name="saleCategoryArea" value="${item.mallUuid}">${item.mallName}
						                    </c:forEach> --%>
                                        </div>
                                    </div>
                                    <div class="form-group" id="sale_product">
                                        <label class="col-lg-2 clo-md-2 control-label">红包销售(商品)：</label>
                                        <div class="col-lg-8" id="productList">
                                        	<c:forEach var="item" items="${mallList}" varStatus="status">
						                             <input type="checkbox" name="saleProductArea" value="${item.mallUuid}">${item.mallName}
						                    </c:forEach>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="clearfix"></div>
                <button type="button" class="btn btn-primary" onclick="cashRed_modify()"
                        style="margin-left:15px;margin-bottom:30px;">发布
                </button>
                <button class="btn btn-default" id="cancel" style="margin-left:15px;margin-bottom:30px;">
                    取消
                </button>
            </form>
        </div>
    </div>
</div>
<script>
	var mallList =[]; 
    $('input[name="saleMallArea"]:checked').each(function(){ 
    	mallList.push($(this).val()); 
    });
    var categoryList =[]; 
    $('input[name="saleCategoryArea"]:checked').each(function(){ 
    	categoryList.push($(this).val()); 
    });
    
	$("#sale_category").hide();
	$("#sale_product").hide();
	
	$('#sale_mall').change(function(e) {
		mallList.splice(0,mallList.length);
		$('input[name="saleMallArea"]:checked').each(function(){ 
	    	mallList.push($(this).val()); 
	    });
		if(mallList.length==0){
			$("#sale_category").hide();
		}else{
			$("#sale_category").show();
		}
		
		
		var baseURL = "${webPath}/category/listOfMall";
	    var json_package = {
	        "head": {},
	        "body": {
	        	"mallList":mallList
	        }
	    }
	    $.post(baseURL, {"json_package": JSON.stringify(json_package)}, function (result) {
	        if (typeof result == 'string') result = JSON.parse(result);
	        console.log(result);
	        if (result.body.result == '1') {
	            $("#categoryList").empty();
	        } else {
	        	$("#categoryList").empty();
	        	var list = result.body.list;
	        	var str = "";
	        	if(list.length<=0){
	        		return
	        	}
	        	for(var i=0;i<list.length;i++){
	        		str += '<input type="checkbox" name="saleCategoryArea" value="'+list[i].id+'">'+list[i].name;
	        	}
	        	$("#categoryList").append(str);
	        }
	    })
	})
	$('#sale_category').change(function(e) {
		categoryList.splice(0,categoryList.length);
		$('input[name="saleCategoryArea"]:checked').each(function(){ 
			categoryList.push($(this).val()); 
	    });
		if(categoryList.length==0){
			$("#sale_product").hide();
		}else{
			$("#sale_product").show();
		}
		
		
		var baseURL = "${webPath}/product/listOfCategory";
	    var json_package = {
	        "head": {},
	        "body": {
	        	"categoryList":categoryList
	        }
	    }
	    $.post(baseURL, {"json_package": JSON.stringify(json_package)}, function (result) {
	        if (typeof result == 'string') result = JSON.parse(result);
	        console.log(result);
	        if (result.body.result == '1') {
	            $("#productList").empty();
	        } else {
	        	$("#productList").empty();
	        	var list = result.body.list;
	        	var str = "";
	        	if(list.length<=0){
	        		return
	        	}
	        	for(var i=0;i<list.length;i++){
	        		str += '<input type="checkbox" name="saleProductArea" value="'+list[i].id+'">'+list[i].name;
	        	}
	        	$("#productList").append(str);
	        }
	    })
	})
	
	var images = [],descs=[];
	var currentI = 0;
	$(function(){
		var cashRedId = "${cashRedId}";
		var baseURL = "${webPath}/cashRed/detail";
	  	var json_package = {
	       "head": {},
	       "body": {
	       	"cashRedId": cashRedId
	       }
	   	}
		$.post(baseURL, {"json_package": JSON.stringify(json_package)}, function (result) {
	      if (typeof result == 'string') result = JSON.parse(result);
	      if (result.body.result == '1') {
	          alert(result.body.description)
	      } else {
	      	var cashRed = result.body.cashRed;
	      	$("#name").val(cashRed.name);
	      	$("#conditionCash").val(cashRed.conditionCash);
	      	$("#rewardCash").val(cashRed.rewardCash);
	      	$("#useCashCondition").val(cashRed.useCashCondition);
	      	$("#useCashResult").val(cashRed.useCashResult);
	      	$("#startTime").val(cashRed.startTime);
	      	$("#endingTime").val(cashRed.endingTime);
	      	$("#createTime").val(cashRed.createTime);
	      	$("#isActive").val(cashRed.isActive);
	      	$("#productId").val(cashRed.productId);
	      	$("#limitCount").val(cashRed.limitCount);
          	
          	$("#typeList").empty();
          	console.log(cashRed.typeList);
          	if(cashRed.typeList != undefined && cashRed.typeList.length>0){
	      		var typeListString = '';
	      		for(var i=0;i<cashRed.typeList.length;i++){
	      			if(cashRed.typeList[i].have == 1){
	      				typeListString += '<option value="'+cashRed.typeList[i].value+'" selected>'+cashRed.typeList[i].name+'</option>';
	      			}else{
	      				typeListString += '<option value="'+cashRed.typeList[i].value+'">'+cashRed.typeList[i].name+'</option>';
	      			}
	      		}
	      		$("#typeList").append(typeListString);
	      	}
          	
          	$("#mallList").empty();
          	var body = result.body;
          	if(body.mallList != undefined && body.mallList.length>0){
	      		var mallListString = '';
	      		for(var i=0;i<body.mallList.length;i++){
	      			if(body.mallList[i].hava == 1){
	      				mallListString += '<input type="checkbox" name="saleMallArea" value="'+body.mallList[i].mallUuid+'" checked>'+body.mallList[i].mallName+" ";
	      			}else{
	      				mallListString += '<input type="checkbox" name="saleMallArea" value="'+body.mallList[i].mallUuid+'">'+body.mallList[i].mallName+" ";
	      			}
	      		}
	      		$("#mallList").append(mallListString);
	      	}
          	
          	$("#sellerList").empty();
          	console.log(body.productSellerList);
          	if(body.productSellerList != undefined && body.productSellerList.length>0){
	      		var productSellerListString = '';
	      		for(var i=0;i<body.productSellerList.length;i++){
	      			if(body.productSellerList[i].hava == 1){
	      				productSellerListString += '<input type="checkbox" name="saleSellerArea" value="'+body.productSellerList[i].id+'" checked>'+body.productSellerList[i].name+" ";
	      			}else{
	      				productSellerListString += '<input type="checkbox" name="saleSellerArea" value="'+body.productSellerList[i].id+'">'+body.productSellerList[i].name+" ";
	      			}
	      		}
	      		$("#sellerList").append(productSellerListString);
	      	}
          	
          	$("#categoryList").empty();
          	if(body.categoryList != undefined && body.categoryList.length>0){
	      		var categoryListString = '';
	      		for(var i=0;i<body.categoryList.length;i++){
	      			if(body.categoryList[i].hava == 1){
	      				categoryListString += '<input type="checkbox" name="saleCategoryArea" value="'+body.categoryList[i].id+'" checked>'+body.categoryList[i].name+" ";
	      			}else{
	      				categoryListString += '<input type="checkbox" name="saleCategoryArea" value="'+body.categoryList[i].id+'">'+body.categoryList[i].name+" ";
	      			}
	      		}
	      		$("#categoryList").append(categoryListString);
	      		$("#sale_category").show();
	      	}
          	
          	$("#productList").empty();
          	if(body.productList != undefined && body.productList.length>0){
	      		var productListString = '';
	      		for(var i=0;i<body.productList.length;i++){
	      			if(body.productList[i].hava == 1){
	      				productListString += '<input type="checkbox" name="saleProductArea" value="'+body.productList[i].id+'" checked>'+body.productList[i].name+" ";
	      			}else{
	      				productListString += '<input type="checkbox" name="saleProductArea" value="'+body.productList[i].id+'">'+body.productList[i].name+" ";
	      			}
	      		}
	      		$("#productList").append(productListString);
	      		$("#sale_product").show();
	      	}
          	
	      }
	  	})
	});
	
	function cashRed_modify() {
		var type = "${type}";
		if(type == "3"){
			alert("当前账号无权限查看此功能");
			return;
		}
		
    	var baseURL = "${webPath}/cashRed/modify";
    	var cashRedId = "${cashRedId}";
    	var name = $('#name').val();
    	var conditionCash = $('#conditionCash').val();
        var rewardCash = $('#rewardCash').val();
        var useCashCondition = $('#useCashCondition').val();
        var useCashResult = $('#useCashResult').val();
        var startTime = $('#startTime').val();
        var endingTime = $('#endingTime').val();
        var productId = $('#productId').val();
        var limitCount = $('#limitCount').val();
        var isActive = $("#isActive").find("option:selected").val();
        var typeList = $("#typeList").find("option:selected").val();
        
        var saleMallArea =[]; 
        $('input[name="saleMallArea"]:checked').each(function(){ 
        	saleMallArea.push($(this).val()); 
        }); 
        var saleSellerArea =[]; 
        $('input[name="saleSellerArea"]:checked').each(function(){ 
        	saleSellerArea.push($(this).val()); 
        }); 
        var saleCategoryArea =[]; 
        $('input[name="saleCategoryArea"]:checked').each(function(){ 
        	saleCategoryArea.push($(this).val()); 
        }); 
        var saleProductArea =[]; 
        $('input[name="saleProductArea"]:checked').each(function(){ 
        	saleProductArea.push($(this).val()); 
        });
        
        var json_package = {
       		"head": {},
            "body": {
            	"cashRedId":cashRedId,
                "name": name,
                "conditionCash": conditionCash,
                "rewardCash": rewardCash,
                "startTime": startTime,
                "endingTime": endingTime,
                "isActive": isActive,
                "useCashCondition": useCashCondition,
                "useCashResult": useCashResult,
                "type": typeList,
                "saleMallArea":saleMallArea,
                "saleSellerArea":saleSellerArea,
                "saleCategoryArea":saleCategoryArea,
                "saleProductArea":saleProductArea,
                "productId":productId,
                "limitCount":limitCount
            }
        };
        $.post(baseURL, {"json_package": JSON.stringify(json_package)}, function (result) {
            if (typeof result == 'string') result = JSON.parse(result);
            if (result.body.result == '0') {
                alert('修改成功！')
                window.location.reload(true);
            } else {
                alert('修改失败！')
            }
        })
    }
	$(function () {
        $('#cancel').click(function () {
            window.location.href = "${webPath}toCashRed";
        })
    });
    $(document).ready(function (e) {
        $('#pro_info').keydown(function () {
            var len = $(this).val().length;
            if (len >= 100) {
                var num = $(this).val().substring(0, 99);
                $(this).val(num);
                alert("超过字数限制")
            }
        })
    });
    $(document).ready(function (e) {
        $('#pro_desc').keydown(function () {
            var len = $(this).val().length;
            if (len >= 140) {
                var num = $(this).val().substring(0, 139);
                $(this).val(num);
                alert("超过字数限制")
            }
        })
    });
    $(function () {
        $('#startTime').datetimepicker({
            format: 'YYYY-MM-DD HH:mm:ss'
        });
        $('#endingTime').datetimepicker({
            format: 'YYYY-MM-DD HH:mm:ss'
        });
        $('#startTime').datetimepicker();
        $('#endingTime').datetimepicker({
            useCurrent: false //Important! See issue #1075
        });
        $("#startTime").on("dp.change", function (e) {
            $('#endingTime').data("DateTimePicker").minDate(e.date);
        });
        $("#endingTime").on("dp.change", function (e) {
            $('#startTime').data("DateTimePicker").maxDate(e.date);
        });
    });
</script>
</body>
</html>